<?php
$title	 = 'Profils Employés';
$this->headTitle($title);
$access	 = array(
    2	 => 'Lecture/Ecriture',
    1	 => 'Lecture',
    0	 => 'Aucun',
);
?>
<div class="content-head">
    <h3><?php echo $title; ?></h3>
    <div class="toolbar">
        <div class="toggle add" title="Ajouter">
            <a href="#" style="display: block; width: 100%; height: 100%" onclick="$('.add-toggle').toggle('slow');
		    return false;"></a>
        </div>
    </div>
</div>
<div class="content-body">
    <div id="msg" class="alert hide"></div>
    <div class="add-toggle">
        <div class="list-container">
            <table class="table table-striped list-table">
                <thead>
                    <tr>
                        <td width="13%" style="text-align: center;">Libellé</td>
                        <td width="10%" style="text-align: center;">Accès Sites</td>
                        <td width="10%" style="text-align: center;">Planning des Managers</td>
                        <td width="10%" style="text-align: center;">Données des Managers</td>
                        <td width="10%" style="text-align: center;">Planning des Employés</td>
                        <td width="10%" style="text-align: center;">Données des Employés</td>
                        <td width="25%" style="text-align: center;">Description</td>
                        <td width="8%">&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td style="text-align: center;">
                            <a hef="#" id="libelle-0" data-type="text">
<?php echo ''; ?>
                            </a>
                        </td>
                        <td style="text-align: center;">
                            <a hef="#" id="site_access-0" data-type="select">
<?php echo $access[0]; ?>
                            </a>
                        </td>
                        <td style="text-align: center;">
                            <a hef="#" id="manager_planning-0" data-type="select">
<?php echo $access[0]; ?>
                            </a>
                        </td>
                        <td style="text-align: center;">
                            <a hef="#" id="manager_data-0" data-type="select">
<?php echo $access[0]; ?>
                            </a>
                        </td>
                        <td style="text-align: center;">
                            <a hef="#" id="employe_planning-0" data-type="select">
<?php echo $access[0]; ?>
                            </a>
                        </td>
                        <td style="text-align: center;">
                            <a hef="#" id="employe_data-0" data-type="select">
<?php echo $access[0]; ?>
                            </a>
                        </td>
                        <td style="text-align: center;">
                            <a href="#" id="description-0" data-type="textarea">
<?php echo ''; ?>
                            </a>
                        </td>
                        <td style='text-align: center;' id='controls-0'>
                            <input type="button" value="Ajouter" class="grh-button" onclick="return add();">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="list-container">
        <table class="table table-striped list-table">
            <thead>
                <tr>
                    <td width="13%" style="text-align: center;">Libellé</td>
                    <td width="10%" style="text-align: center;">Accès Sites</td>
                    <td width="10%" style="text-align: center;">Planning des Managers</td>
                    <td width="10%" style="text-align: center;">Données des Managers</td>
                    <td width="10%" style="text-align: center;">Planning des Employés</td>
                    <td width="10%" style="text-align: center;">Données des Employés</td>
                    <td width="25%" style="text-align: center;">Description</td>
                    <td width="8%">&nbsp;</td>
                </tr>
            </thead>
            <tbody>
<?php foreach ($this->profilList as $profil): ?>
                    <tr>
                        <td>
			    <a hef="#" id="libelle-<?php echo $profil->id ?>" class="edit-libelle" data-pk="<?php echo $profil->id ?>" data-name="libelle" data-type="text">
    <?php echo $profil->libelle; ?>
                            </a>
                        </td>
                        <td>
			    <a hef="#" id="site_access-<?php echo $profil->id ?>" class="edit-site_access" data-pk="<?php echo $profil->id ?>" data-name="site_access" data-type="select">
    <?php echo $access[(int) $profil->site_access]; ?>
                            </a>
                        </td>
                        <td>
			    <a hef="#" id="manager_planning-<?php echo $profil->id ?>" class="edit-manager_planning" data-pk="<?php echo $profil->id ?>" data-name="manager_planning" data-type="select">
    <?php echo $access[(int) $profil->manager_planning]; ?>
                            </a>
                        </td>
                        <td>
			    <a hef="#" id="manager_data-<?php echo $profil->id ?>" class="edit-manager_data" data-pk="<?php echo $profil->id ?>" data-name="manager_data" data-type="select">
    <?php echo $access[(int) $profil->manager_data]; ?>
                            </a>
                        </td>
                        <td>
			    <a hef="#" id="employe_planning-<?php echo $profil->id ?>" class="edit-employe_planning" data-pk="<?php echo $profil->id ?>" data-name="employe_planning" data-type="select">
    <?php echo $access[(int) $profil->employe_planning]; ?>
                            </a>
                        </td>
                        <td>
			    <a hef="#" id="employe_data-<?php echo $profil->id ?>" class="edit-employe_data" data-pk="<?php echo $profil->id ?>" data-name="employe_data" data-type="select">
    <?php echo $access[(int) $profil->employe_data]; ?>
                            </a>
                        </td>
                        <td>
			    <a href="#" id="description-<?php echo $profil->id ?>" class="edit-description" data-pk="<?php echo $profil->id ?>" data-name="description" data-type="textarea">
    <?php echo $profil->description; ?>
                            </a>
                        </td>
                                <td style='text-align: center;' id='controls-<?php echo $profil->id ?>'>
        <!--                                <a class='grh-edit' href='#' onclick='return edit(<?php echo $profil->id ?>);'>
    			<img src='<?php echo $this->basePath() . '/img/grh/edit-16x15.png' ?>' alt='Editer' title='Editer'>
		    </a>-->
    				    &nbsp;&nbsp;
                                    <a class='grh-delete' href='#' onclick="return erase(<?php echo $profil->id ?>);">
                                <img src='<?php echo $this->basePath() . '/img/grh/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
                            </a>
	<!--                                    <a class='grh-save' style='display: none;' href='#' onclick='return save(<?php echo $profil->id ?>);'>
    					<img src="<?php echo $this->basePath() . '/img/foam/enregistrer.png' ?>" alt="Enregistrer" title="Enregistrer">
				    </a>-->
    				</td>
                    </tr>
<?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript">
		$(document).ready(function() {
		    $("#libelle-0").editable({
			title: 'Edition du Libellé'
		    });
		    $("#site_access-0").editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#manager_planning-0").editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#manager_data-0").editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#employe_planning-0").editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#employe_data-0").editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#description-0").editable({
			title: 'Edition de la description'
		    });
		    // Editable
		    $('.edit-libelle').editable({
			title: "Edition du Libellé",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveStringData')); ?>"
		    });
		    $('.edit-site_access').editable({
			title: "Edition des Droits d'accès sur les Sites",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveIntData')); ?>",
			source: '<?php echo json_encode($access); ?>'
		    });
		    $('.edit-manager_planning').editable({
			title: "Edition des Droits d'accès sur les Plannings des Managers",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveIntData')); ?>",
			source: '<?php echo json_encode($access); ?>'
		    });
		    $('.edit-manager_data').editable({
			title: "Edition des Droits d'accès sur les Données des Managers",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveIntData')); ?>",
			source: '<?php echo json_encode($access); ?>'
		    });
		    $('.edit-employe_planning').editable({
			title: "Edition des Droits d'accès sur les Plannings des Employés",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveIntData')); ?>",
			source: '<?php echo json_encode($access); ?>'
		    });
		    $('.edit-employe_data').editable({
			title: "Edition des Droits d'accès sur les Données des Employés",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveIntData')); ?>",
			source: '<?php echo json_encode($access); ?>'
		    });
		    $('.edit-description').editable({
			title: "Edition de la Description",
			url : "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'saveStringData')); ?>",
		    });
		});

		function add()
		{
		    var accessText = new Array();
		    accessText['Aucun'] = 0;
		    accessText['Lecture'] = 1;
		    accessText['Lecture/Ecriture'] = 2;
		    var libelleText = $.trim($("#libelle-0").text());
		    var site_accessText = accessText[$.trim($("#site_access-0").text())];
		    var manager_planningText = accessText[$.trim($("#manager_planning-0").text())];
		    var manager_dataText = accessText[$.trim($("#manager_data-0").text())];
		    var employe_planningText = accessText[$.trim($("#employe_planning-0").text())];
		    var employe_dataText = accessText[$.trim($("#employe_data-0").text())];
		    var descriptionText = $.trim($("#description-0").text());
		    var formData = "id=0&libelle=" + libelleText + "&site_access=" + site_accessText + "&manager_planning=" + manager_planningText + "&manager_data=" + manager_dataText + "&employe_planning=" + employe_planningText + "&employe_data=" + employe_dataText + "&description=" + descriptionText;
		    //alert(formData);
		    var ajax = $.ajax({
			type: 'post',
			url: '<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'add')) ?>',
			data: formData
		    });
		    ajax.success(function(result) {
			if(!result.done){
			    alert("Une erreur est survenue lors de la génération du nouveau profil");
			}
			location.reload();
		    });
		    ajax.error(function() {
			alert("Une erreur est survenue !");
		    });
		    return false;
		}

		function edit(id)
		{
		    $("#libelle-" + id).editable({
			title: 'Edition du Libellé'
		    });
		    $("#site_access-" + id).editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#manager_planning-" + id).editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#manager_data-" + id).editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#employe_planning-" + id).editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#employe_data-" + id).editable({
			title: 'Edition des Droits',
			source: [{value: 0, text: 'Aucun'}, {value: 1, text: 'Lecture'}, {value: 2, text: 'Lecture/Ecriture'}]
		    });
		    $("#description-" + id).editable({
			title: 'Edition de la description'
		    });
		    RHtoggleEdit(id);
		    return false;
		}

		function erase(id)
		{
		    var conf = confirm("Voulez-vous effacer le Profil ?");
		    if (conf != true)
			return false;
		    var ajax = $.ajax({
			type: 'post',
			url: "<?php echo $this->url('RHX/default', array('controller'	 => 'profil', 'action'	 => 'erase')); ?>",
			data: '&id=' + id
		    });
		    ajax.success(function(result) {
			if(result.nb > 0){
			    //alert('Effacé');
			}else{
			    alert('Erreur');
			}
			location.reload();
		    });
		    ajax.error(function() {
			alert("Une erreur est survenue")
		    });
		    return false;
		}

		function save(id)
		{
		    var accessText = new Array();
		    accessText['Aucun'] = 0;
		    accessText['Lecture'] = 1;
		    accessText['Lecture/Ecriture'] = 2;
		    var libelleText = $.trim($("#libelle-" + id).text());
		    var site_accessText = accessText[$.trim($("#site_access-" + id).text())];
		    var manager_planningText = accessText[$.trim($("#manager_planning-" + id).text())];
		    var manager_dataText = accessText[$.trim($("#manager_data-" + id).text())];
		    var employe_planningText = accessText[$.trim($("#employe_planning-" + id).text())];
		    var employe_dataText = accessText[$.trim($("#employe_data-" + id).text())];
		    var descriptionText = $.trim($("#description-" + id).text());
		    var formData = "id=" + id + "&libelle=" + libelleText + "&site_access=" + site_accessText + "&manager_planning=" + manager_planningText + "&manager_data=" + manager_dataText + "&employe_planning=" + employe_planningText + "&employe_data=" + employe_dataText + "&description=" + descriptionText;
		    //alert(formData);
		    var ajax = $.ajax({
			type: 'post',
			url: '<?php echo $this->url('RH/default', array('controller'	 => 'profil', 'action'	 => 'edit')) ?>',
			data: formData
		    });
		    ajax.success(function() {
			$("#libelle-" + id).editable('toggleDisabled');
			$("#site_access-" + id).editable('toggleDisabled');
			$("#manager_planning-" + id).editable('toggleDisabled');
			$("#manager_data-" + id).editable('toggleDisabled');
			$("#employe_planning-" + id).editable('toggleDisabled');
			$("#employe_data-" + id).editable('toggleDisabled');
			$("#description-" + id).editable('toggleDisabled');
			RHtoggleEdit(id);
		    });
		    ajax.error(function() {
			alert("Une erreur est survenue !");
		    });
		    return false;
		}
</script>